<template>
  <Layout>
    <!-- Page Header -->
    <header class="masthead" style="background-image: url('/img/contact-bg.jpg')">
      <div class="overlay"></div>
      <div class="container">
        <div class="row">
          <div class="col-lg-8 col-md-10 mx-auto">
            <div class="page-heading">
              <h1>Contact Me</h1>
              <span class="subheading">Have questions? I have answers.</span>
            </div>
          </div>
        </div>
      </div>
    </header>

    <!-- Main Content -->
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <p>Want to get in touch? Fill out the form below to send me a message and I will get back to you as soon as possible!</p>
          <!-- Contact Form - Enter your email address on line 19 of the mail/contact_me.php file to make this form work. -->
          <!-- WARNING: Some web hosts do not allow emails to be sent through forms to common mail hosts like Gmail or Yahoo. It's recommended that you use a private domain email address! -->
          <!-- To use the contact form, your site must be on a live web host with PHP! The form will not work locally! -->
          <form name="sentMessage" id="contactForm" novalidate>
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">
                <label>Name</label>
                <input type="text" v-model="form.name" class="form-control" placeholder="Name" id="name" required data-validation-required-message="Please enter your name.">
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">
                <label>Email Address</label>
                <input type="email" v-model="form.email" class="form-control" placeholder="Email Address" id="email" required data-validation-required-message="Please enter your email address.">
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <div class="control-group">
              <div class="form-group col-xs-12 floating-label-form-group controls">
                <label>Phone Number</label>
                <input type="tel" v-model="form.phone" class="form-control" placeholder="Phone Number" id="phone" required data-validation-required-message="Please enter your phone number.">
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <div class="control-group">
              <div class="form-group floating-label-form-group controls">
                <label>Message</label>
                <textarea v-model="form.message" rows="5" class="form-control" placeholder="Message" id="message" required data-validation-required-message="Please enter a message."></textarea>
                <p class="help-block text-danger"></p>
              </div>
            </div>
            <br>
            <div id="success"></div>
            <button type="button" class="btn btn-primary" id="sendMessageButton" @click="onSubmit">Send</button>
          </form>
        </div>
      </div>
    </div>
  </Layout>
</template>

<script>
  import axios from 'axios'

  export default {
    name:'ContactPage',
    data () {
      return {
        form: {
          name: '',
          email: '',
          phone: '',
          message: ''
        }
      };
    },

    mounted() {},

    methods: {
      async onSubmit () {
        if(!this.form.name){
          alert("请输入名字")
          return
        }
        if(!this.form.email){
          alert("请输入邮箱")
          return
        }
        const reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
        if(!reg.test(this.form.email)){
          alert("邮箱格式不正确")
          return
        }
        if(!this.form.phone){
          alert("请输入手机号码")
          return
        }
        const regPhone = /^1(3|4|5|7|8)\d{9}$/;
        if(!regPhone.test(this.form.phone)){
          alert("手机号码格式不正确")
          return
        }
        if(!this.form.message){
          alert("请输入信息")
          return
        }
        try {
          const { data } = await axios({
            method: 'POST',
            url: 'http://localhost:1337/contacts',
            data: this.form
          })
          alert('发送成功')
          this.form = {
            name: '',
            email: '',
            phone: '',
            message: ''
          }
        } catch (err) {
          alert('发送失败')
        }
      }
    },

    watch: {}

  }

</script>
<style>

</style>